package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/5/6.
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        roleMapper.deleteRelation(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        //往中间表role_permission中添加关系数据
        //批量添加,一次执行多条sql语句
        if (ids != null && ids.length>0) {
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        //批量添加,一次执行多条sql语句
        if (ids != null && ids.length>0) {
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }


    public PageInfo<Role> query(QueryObject qo) {
//        int count = roleMapper.queryForCount(qo);
//        if (count == 0){
//            return PageResult.empty(qo.getPageSize());
//        }
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.queryForList(qo);
        return new PageInfo(roles);
    }
}
